package string.leetcode.medium;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author bruin_du
 * @description 字符串中的所有变位词
 * @date 2022/8/4 17:15
 **/
public class OfferII015_FindAnagrams {
    public List<Integer> findAnagrams(String s, String p) {
        int len1 = s.length(), len2 = p.length();
        List<Integer> list = new ArrayList<>();
        if (len1 < len2) return list;
        int[] arr1 = new int[26];
        int[] arr2 = new int[26];
        for (int i = 0; i < len2; i++) {
            ++arr1[s.charAt(i) - 'a'];
            ++arr2[p.charAt(i) - 'a'];
        }
        if (Arrays.equals(arr1, arr2)) list.add(0);
        for (int i = len2; i < len1; i++) {
            ++arr1[s.charAt(i) - 'a'];
            --arr1[s.charAt(i - len2) - 'a'];
            if (Arrays.equals(arr1, arr2)) list.add(i - len2 + 1);
        }
        return list;
    }
}
